package zhh.tangbao.syslog.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import zhh.tangbao.syslog.dao.WorkRecordDao;
import zhh.tangbao.syslog.dto.WorkRecordQueryDto;
import zhh.tangbao.syslog.entity.WorkRecord;
import zhh.tangbao.syslog.service.WorkRecordService;
import zhh.tangbao.wechat.dao.WeChatUserDao;
import zhh.tangbao.wechat.entity.WeChatUser;
import zhh.tangbao.wechat.vo.simple.UserIdAndPhoneVo;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author 粥灰灰
 * <p>2023/4/25</p>
 */

@Service
public class WorkRecordServiceImpl implements WorkRecordService {

    @Autowired
    private WorkRecordDao dao;

    @Autowired
    private WeChatUserDao weChatUserDao;

    @Override
    public Page<WorkRecord> page(WorkRecordQueryDto dto) {
        Page<WorkRecord> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<WorkRecord> query = new QueryWrapper<>();
        query.orderByDesc("record_time");
        if (StrUtil.isNotBlank(dto.getTitle())) {
            query.like("title", dto.getTitle());
        }
        Map<String, String> recordUsersAccount = Maps.newConcurrentMap();
        if (StrUtil.isNotBlank(dto.getPhoneNumber())) {
            WeChatUser weChatUser = weChatUserDao.selectOne(new QueryWrapper<WeChatUser>().eq("phone_number", dto.getPhoneNumber()));
            Assert.notNull(weChatUser, "该用户信息已被删除，无法通过电话号码查询工作记录");
            recordUsersAccount.put(weChatUser.getId(), weChatUser.getId());
            query.eq("record_user_id", weChatUser.getId());
        }
        if (null != dto.getSubmitTimeStart() && null != dto.getSubmitTimeEnd()) {
            query.between("record_time", dto.getSubmitTimeStart(), dto.getSubmitTimeEnd());
        }
        page = dao.selectPage(page, query);
        List<WorkRecord> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return page;
        }
        if (StrUtil.isBlank(dto.getPhoneNumber())) {
            List<UserIdAndPhoneVo> weChatUsers = weChatUserDao.listUserIdAndPhoneVoByIds(records.stream().map(WorkRecord::getRecordUserId).collect(Collectors.toList()));
            if (CollUtil.isEmpty(weChatUsers)) {
                return page;
            }
            for (int i = 0; i < weChatUsers.size(); i++) {
                UserIdAndPhoneVo vo = weChatUsers.get(i);
                recordUsersAccount.put(vo.getId(), vo.getPhone());
            }
        }
        for (int i = 0; i < records.size(); i++) {
            WorkRecord r = records.get(i);
            r.setRecordUserAccount(recordUsersAccount.get(r.getRecordUserId()));
        }
        return page;
    }
}
